package com.website.core.aop;

import com.website.common.controller.BaseController;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class RequestMyAop extends BaseController {
    @Aspect
    @Component
    public class HandlingTimeAspect {
        Logger logger = LoggerFactory.getLogger(RequestMyAop.class);
        //开始时间
        private long starttime;
        @Pointcut("@annotation(MyAop)")
        public void handlingTimePointcut() {
        }

        @Around("handlingTimePointcut()")
        public Object handlingTimeAround(ProceedingJoinPoint joinPoint) {
            long starttime = System.currentTimeMillis();
            Object obj = null;
            try {
                obj = joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            long endtime = System.currentTimeMillis();
            Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
            String methodName = method.getName();
            logger.info(methodName + "自定义方法执行了" + (endtime - starttime) + "ms");
            return obj;
        }
    }
}
